package com.xiaomengkuan.comment.controller;

import com.xiaomengkuan.comment.entity.vo.CommentResponseVo;
import com.xiaomengkuan.comment.mapper.CommentMapper;
import com.xiaomengkuan.comment.service.CommentService;
import com.xiaomengkuan.comment.entity.ro.CommentPageRo;
import com.xiaomengkuan.common.entity.vo.PageVo;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-27 1:33 下午, File Encoding: utf8 --
 */
@RestController
@Api(tags = "评论管理")
@RequestMapping("comment/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private CommentMapper commentMapper;

    /**
     * 获取商品的所有评论
     *
     * @param id 商品id
     * @return R
     */
    @ApiOperation(value = "获取商品的所有评论")
    @GetMapping("getProductComments/{id}")
    public R getProductComments(@PathVariable("id") String id) {
        List<CommentResponseVo> list = commentService.getProductComments(id);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putList(list);
    }

    /**
     * 分页获取商品的评论
     *
     * @param commentPageRo size current productId
     * @return R
     */
    @ApiOperation(value = "分页获取商品的评论")
    @PostMapping("getCommentsPage")
    public R getCommentsPage(@RequestBody CommentPageRo commentPageRo) {
        PageVo<CommentResponseVo> pageVo = commentService.page(commentPageRo);
        return pageVo != null ?
                R.ok().detail(DetailSelector.QUERY_SUCCESS).page(pageVo) :
                R.err().detail(DetailSelector.QUERY_ERROR);
    }

    @ApiOperation(value = "获取回复的评论")
    @PostMapping("getRelayCommentsPage")
    public R getRelayCommentsPage(@RequestBody Map<String, Object> requestBody) {
        PageVo<CommentResponseVo> pageVo =
                commentService.getReplayCommentsPage(requestBody);
        return pageVo != null ?
                R.ok().detail(DetailSelector.QUERY_SUCCESS).page(pageVo) :
                R.err().detail(DetailSelector.QUERY_ERROR);
    }

    /**
     * 发布评论
     *
     * @param requestBody 请求体
     * @return R
     */
    @ApiOperation(value = "发布评论")
    @PostMapping("commit")
    public R commit(@RequestBody Map<String, Object> requestBody) {
        boolean flag = commentService.commit(requestBody);
        return flag ? R.ok().detail(DetailSelector.INSERT_SUCCESS) :
                R.err().detail(DetailSelector.INSERT_ERROR);
    }

    /**
     * 回复
     *
     * @param requestBody 请求体
     * @return R
     */
    @ApiOperation(value = "回复")
    @PostMapping("replay")
    public R replay(@RequestBody Map<String, String> requestBody) {
        boolean flag = commentService.replay(requestBody);
        return flag ? R.ok().detail(DetailSelector.INSERT_SUCCESS) :
                R.err().detail(DetailSelector.INSERT_ERROR);
    }

    @ApiOperation(value = "获取用户评论数")
    @GetMapping("commentCount/{userId}")
    public R commentCount(@PathVariable String userId) {
        Integer count = commentService.commentCount(userId);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putItem(count);
    }
}
